package com.violet.detective.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.violet.detective.entity.Equipment;
import com.violet.detective.entity.Repair;
import com.violet.detective.service.EquipmentService;
import com.violet.detective.service.RepairService;
import com.violet.detective.mapper.RepairMapper;
import com.violet.detective.search.RepairSearchParameter;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author 21145
* @description 针对表【Repair】的数据库操作Service实现
* @createDate 2023-09-04 21:42:38
*/
@Service
public class RepairServiceImpl extends ServiceImpl<RepairMapper, Repair>
    implements RepairService{

    @Resource
    RepairMapper repairMapper;

    @Resource
    private EquipmentService equipmentService;

    public boolean addRepair(Repair repair) {
        int result = repairMapper.insert(repair);
        return result > 0;
    }

    public boolean editRepair(Repair updatedRepair) {
        int result = repairMapper.updateById(updatedRepair);
        return result > 0;
    }

    public boolean deleteRepair(Integer id) {
        int result = repairMapper.deleteById(id);
        return result > 0;
    }

    public Repair getRepairById(Integer id) {
        return repairMapper.selectById(id);
    }

    public List<Repair> findByEquipmentId(int equipmentId) {
        QueryWrapper<Repair> wrapper = new QueryWrapper<>();
        wrapper.eq("equipmentId", equipmentId);
        return repairMapper.selectList(wrapper);
    }

    public List<Repair> findByEquipmentName(String equipmentName) {
        QueryWrapper<Equipment> equipmentWrapper = new QueryWrapper<>();
        equipmentWrapper.like("name", equipmentName);
        List<Equipment> equipments = equipmentService.list(equipmentWrapper);

        if (equipments.isEmpty())
            return Collections.emptyList();
        else {
            List<Repair> repairs = new ArrayList<>();
            for (Equipment equipment : equipments) {
                QueryWrapper<Repair> repairWrapper = new QueryWrapper<>();
                repairWrapper.eq("equipmentId", equipment.getId());
                repairs.addAll(repairMapper.selectList(repairWrapper));
            }
            return repairs;
        }
    }

    public List<Repair> findByType(String type) {
        QueryWrapper<Repair> wrapper = new QueryWrapper<>();
        wrapper.eq("type", type);
        return repairMapper.selectList(wrapper);
    }

    public List<Repair> findByLubricationPart(String lubricationPart) {
        QueryWrapper<Repair> wrapper = new QueryWrapper<>();
        wrapper.eq("lubricationPart", lubricationPart);
        return repairMapper.selectList(wrapper);
    }

    public List<Repair> findByOilSupplyType(String oilType) {
        QueryWrapper<Repair> wrapper = new QueryWrapper<>();
        wrapper.eq("oilSupplyType", oilType);
        return repairMapper.selectList(wrapper);
    }

    public List<Repair> findByLubricationMethod(String lubricationMethod) {
        QueryWrapper<Repair> wrapper = new QueryWrapper<>();
        wrapper.eq("lubricationMethod", lubricationMethod);
        return repairMapper.selectList(wrapper);
    }

    public List<Repair> searchRepairs(RepairSearchParameter searchParameter) {
        QueryWrapper<Repair> query = new QueryWrapper<>();
        if (searchParameter.getEquipmentId() != null)  //设备编码
            query.eq("equipmentId", searchParameter.getEquipmentId());
        if (searchParameter.getType() != null)  //类型
            query.eq("type", searchParameter.getType());
        if (searchParameter.getLubricationPart() != null)  //润滑部位
            query.eq("lubricationPart", searchParameter.getLubricationPart());
        if (searchParameter.getOilSupplyType() != null)  //给油脂方式
            query.eq("oilSupplyType", searchParameter.getOilSupplyType());
        if (searchParameter.getLubricationMethod() != null)  //润滑方式
            query.eq("lubricationMethod", searchParameter.getLubricationMethod());
        List<Repair> lst1 =  repairMapper.selectList(query);
        if (searchParameter.getEquipmentName() != null) {
            List<Integer> lst2 = equipmentService.searchEquipmentIdsByName(searchParameter.getEquipmentName());
            return lst1.stream()
                    .filter(repair -> lst2.contains(repair.getEquipmentId()))
                    .collect(Collectors.toList());
        }
        return lst1;
    }

    public Page<Repair> searchRepairs(RepairSearchParameter searchParameter, int pageNum, int pageSize) {
        QueryWrapper<Repair> query = new QueryWrapper<>();
        if (searchParameter.getEquipmentId() != null)  //设备编码
            query.eq("equipmentId", searchParameter.getEquipmentId());
        if (searchParameter.getType() != null)  //类型
            query.eq("type", searchParameter.getType());
        if (searchParameter.getLubricationPart() != null)  //润滑部位
            query.eq("lubricationPart", searchParameter.getLubricationPart());
        if (searchParameter.getOilSupplyType() != null)  //给油脂方式
            query.eq("oilSupplyType", searchParameter.getOilSupplyType());
        if (searchParameter.getLubricationMethod() != null)  //润滑方式
            query.eq("lubricationMethod", searchParameter.getLubricationMethod());

        if (searchParameter.getEquipmentName() != null) {  //设备名称
            List<Integer> lst2 = equipmentService.searchEquipmentIdsByName(searchParameter.getEquipmentName());
            if (!lst2.isEmpty()) {
                query.in("equipmentId", lst2);
            } else {
                return new Page<>();
            }
        }

        Page<Repair> page = new Page<>(pageNum, pageSize);
        return repairMapper.selectPage(page, query);
    }


    public List<Repair> findAllRepairs() {
        return repairMapper.selectList(null);
    }

    public Page<Repair> findAllRepairs(int pageNum, int pageSize) {
        Page<Repair> page = new Page<>(pageNum, pageSize);
        return repairMapper.selectPage(page, null);
    }
}


